package com.spotalog.shared.model;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;

@Data
@XmlRootElement
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Radius {
	private Location center;
	private double radius;

	/**
	 * For JAX-RS compliance
	 * 
	 * @param asString
	 * @return
	 */
	public static Radius valueOf(String asString) {
		List<String> coords = ImmutableList.copyOf(Splitter.on(",").split(
				asString));
		Preconditions
				.checkArgument(coords.size() >= 3,
						"Cannot create Radius from [%s]! Must have 3 doubles (LAT,LNG,RAD)");
		double lat = Double.valueOf(coords.get(0));
		double lng = Double.valueOf(coords.get(1));
		double radius = Double.valueOf(coords.get(2));
		return new Radius(new Location(lat, lng), radius);
	}
}
